package cn.itsource.sm.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageBean;
import cn.itsource.sm.domain.Systemdictionarydetail;
import cn.itsource.sm.query.SystemdictionarydetailQuery;
import cn.itsource.sm.service.ISystemdictionarydetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemdictionarydetail")
public class SystemdictionarydetailController {

    @Autowired
    private ISystemdictionarydetailService systemdictionarydetailService;

    /**
     * 根据id查询单个对象：get localhost:8080/systemdictionarydetail/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Systemdictionarydetail findOne(@PathVariable("id") Long id) {
        return systemdictionarydetailService.selectById(id);
    }

    /**
     * 查询所有对象：get localhost:8080/systemdictionarydetail
     * @return
     */
    @GetMapping
    public List<Systemdictionarydetail> findAll() {
        return systemdictionarydetailService.selectAll();
    }

    /**
     * 添加和修改：Put localhost:8080/systemdictionarydetail
     * @param systemdictionarydetail
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Systemdictionarydetail systemdictionarydetail) {
        try {
            if (systemdictionarydetail.getId() == null) {
                systemdictionarydetailService.save(systemdictionarydetail);
            } else {
                systemdictionarydetailService.update(systemdictionarydetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            systemdictionarydetailService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败！");
        }
    }

    @PostMapping
    public PageBean<Systemdictionarydetail> pageQuery(@RequestBody SystemdictionarydetailQuery systemdictionarydetailQuery) {
        return systemdictionarydetailService.queryPage(systemdictionarydetailQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            systemdictionarydetailService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败！");
        }
    }

}